package com.example.zx.theline.jclr.pub;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zx.theline.R;
import com.example.zx.theline.jclr.JclrActivity;
import com.example.zx.theline.sgjh.pub.EditorGuardianActivity;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import GsonBean.JcbShdwListBean;
import GsonBean.JcbZrDwListBean;
import GsonBean.JdjclrxqBean;
import GsonBean.RjhsgxmList;
import GsonBean.SuccessBean;
import GsonBean.post.SaveCaoGaoPost;
import Utils.RefreshManager;
import Utils.StatusUtil;
import app.MyApplication;
import automatchingview.AutoMatchingPopuWindow;
import base.BaseActivity;
import dialog.IsFinishDialog;
import dialog.TjwcDialog;
import greendao.UserDao;
import greendao.entity.User;
import me.xiaopan.android.widget.LinearLineWrapLayout;
import network.NetWorkApi;
import network.ObjectHttpResponseHandler;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import views.AutoMatchingPopuClickListener;
import views.LzxxItemView;
import Utils.MyToast;
import views.RiskEvaluationView;

import static Utils.StatusUtil.STATIUS_TGQX_1_TGYS_BTY;

public class JcxxAddActivity extends BaseActivity {
    private TextView centerTextView, leftTextView;

    RadioButton jhxx;
    RadioButton jcxx;

    LinearLayout radiogroup_layout;

    //控制检查详情 layout visiavle
    RadioButton jcxx_button;

    //流转信息详情 layout visiavle
    RadioButton lzxx_button;
    //日计划编号
    String rjdjhbBh;

    //检查表ID
    String jdjhjcbId;

    //月度编号
    String ydbh;

    //日计划号
    TextView rjhh;

    //日计划号
    TextView ydbhtv;

    //类别
    TextView lb;

    Spinner zrdw;

    TextView sgdw;
    TextView sgdd;
    TextView sgxm;
    TextView jcrq;
    TextView jcrtitle;
    EditText jcr;
    TextView jhjhr;
    TextView lrr;
    EditText jcdd;
    LinearLayout fxlb;
    //风险列表 view
    RiskEvaluationView mRiskEvaluationView;
    TextView ztpj;
    EditText jcqk;

    ScrollView lzxx_layout;
    ScrollView jcxx_layout;


    //提交并整改
    TextView tjbzg;
    //停工整改
    TextView tgzg;
    //检查完成
    TextView jcwc;
    //保存草稿
    TextView bccg;
    //删除草稿
    TextView sccg;
    //取消录入
    TextView qxlr;

    List<ZrdwData> SpinnerData;

    LinearLayout lzxx;
    LinearLayout LinearLayout;


    IsFinishDialog mIsFinishDialog;
    TjwcDialog mTjwcDialog;

    //责任单位 spinner默认选中项 position
    int SpinnerPosition;

    //若检查表总体评价为有问题，显示改安全问题模块
    LinearLineWrapLayout aqwt_layout;

    boolean hasProblem = false;

    JdjclrxqBean mJdjclrxqBean;
    //本单位监护人员
    List<JdjclrxqBean.SgjhrBean> mUnitPerson = new ArrayList<>();
    List<JdjclrxqBean.SgjhrBean> mCheckList = new ArrayList<>();

    private PopupWindow mPopu;
    private AutoMatchingPopuWindow window;

    private PopupWindow mPopu2;
    private AutoMatchingPopuWindow window2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jcxx_add);
        InitView();
        InitData();
        InitListen();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void InitView() {
        centerTextView = (TextView) findViewById(R.id.centerTextView);
        leftTextView = (TextView) findViewById(R.id.leftTextView);
        rjhh = (TextView) findViewById(R.id.rjhh);
        ydbhtv = (TextView) findViewById(R.id.ydbh);
        lb = (TextView) findViewById(R.id.lb);
        zrdw = (Spinner) findViewById(R.id.zrdw);
        sgdw = (TextView) findViewById(R.id.sgdw);
        sgdd = (TextView) findViewById(R.id.sgdd);
        sgxm = (TextView) findViewById(R.id.sgxm);
        sgdd = (TextView) findViewById(R.id.sgdd);
        jcrq = (TextView) findViewById(R.id.jcrq);
        jcrtitle = (TextView) findViewById(R.id.jcrtitle);
        jcr = (EditText) findViewById(R.id.jcr);
        lrr = (TextView) findViewById(R.id.lrr);
        jcdd = (EditText) findViewById(R.id.jcdd);
        fxlb = (LinearLayout) findViewById(R.id.fxlb);
        ztpj = (TextView) findViewById(R.id.ztpj);
        jcqk = (EditText) findViewById(R.id.jcqk);
        jhjhr = (TextView) findViewById(R.id.jhjhr);


        tjbzg = (TextView) findViewById(R.id.tjbzg);
        tgzg = (TextView) findViewById(R.id.tgzg);
        jcwc = (TextView) findViewById(R.id.jcwc);
        bccg = (TextView) findViewById(R.id.bccg);
        sccg = (TextView) findViewById(R.id.sccg);
        qxlr = (TextView) findViewById(R.id.qxlr);


        radiogroup_layout = (LinearLayout) findViewById(R.id.radiogroup_layout);
        jhxx = (RadioButton) findViewById(R.id.radiobutton1);
        jcxx = (RadioButton) findViewById(R.id.radiobutton2);
        jcxx_button = (RadioButton) findViewById(R.id.jcxx_button);
        lzxx_button = (RadioButton) findViewById(R.id.lzxx_button);

        lzxx_layout = (ScrollView) findViewById(R.id.lzxx_layout);
        jcxx_layout = (ScrollView) findViewById(R.id.jcxx_layout);
        lzxx = (LinearLayout) findViewById(R.id.lzxx);
        LinearLayout = (LinearLayout) findViewById(R.id.LinearLayout);

        aqwt_layout = (LinearLineWrapLayout) findViewById(R.id.aqwt_layout);

    }

    @Override
    protected void InitData() {
        centerTextView.setText("新增检查单");
        Bundle mBundle = getIntent().getExtras();
        rjdjhbBh = mBundle.getString("rjdjhbBh");
        jdjhjcbId = mBundle.getString("jdjnjcbId");

        if (mBundle.getBoolean("hideSaveCaoGaoBut")) {
            hideSaveCaoGaoBut();
        }

        //日计划号：rjdjhbBh
        //录入人单位ID：lrdwId（即登录人单位ID）
        //录入人姓名：lrrname（即登录人真实姓名）
        //传三个参数
        Map<String, String> map = new HashMap<>();
        map.put("rjdjhbBh", rjdjhbBh);
        map.put("lrdwId", MyApplication.getmLoginUser().getUserUnitId());
        map.put("lrrname", MyApplication.getmLoginUser().getTrueName());
        if (jdjhjcbId != null) {
            map.put("jdjhjcbId", jdjhjcbId);
        }
        MyApplication.ShowWaitDialog(this);
        NetWorkApi.getJdjcjhInfo(map, new Callback<JdjclrxqBean>() {
                    @Override
                    public void onResponse(Call<JdjclrxqBean> call, Response<JdjclrxqBean> response) {
                        try {
                            mJdjclrxqBean = response.body();
                            //月度编号
                            ydbh = mJdjclrxqBean.getRjdjhbInfo().getYdbh();
                            ydbhtv.setText(ydbh);
                            //类别
                            lb.setText(mJdjclrxqBean.getRjdjhbInfo().getLb());
                            //日计划号
                            rjhh.setText(mJdjclrxqBean.getRjdjhbInfo().getBh());
                            //设置spinnerData 并设置默认选中项
                            SpinnerData = new ArrayList<ZrdwData>();
                            for (int j = 0; j < mJdjclrxqBean.getJcbZrDwList().size(); j++) {
                                JcbZrDwListBean mBean = mJdjclrxqBean.getJcbZrDwList().get(j);
                                ZrdwData data = new ZrdwData();
                                data.dwmc = mBean.getDwmc();
                                data.dwid = mBean.getDwId();
                                SpinnerData.add(data);
                                if (data.dwid.equals(mJdjclrxqBean.getRjdjhjcbInfo().getZrdwId())) {
                                    SpinnerPosition = j;
                                }
                            }
                            zrdw.setAdapter(new ArrayAdapter<ZrdwData>(JcxxAddActivity.this, android.R.layout.simple_spinner_item, SpinnerData));
                            zrdw.setSelection(SpinnerPosition);
                            //施工单位
                            sgdw.setText(mJdjclrxqBean.getRjdjhbInfo().getSgdwmc());
                            //施工地点
                            sgdd.setText(mJdjclrxqBean.getRjdjhbInfo().getSgdd());
                            //施工项目
                            sgxm.setText(mJdjclrxqBean.getRjdjhbInfo().getXmmc());
                            //录入人 为登录用户 无法修改
                            lrr.setText(MyApplication.getmLoginUser().getTrueName());
                            //风险列表
                            mRiskEvaluationView = new RiskEvaluationView(JcxxAddActivity.this, mJdjclrxqBean.getJcbAqfxList(), false, new RiskEvaluationView.OnItemClickListen() {
                                @Override
                                public void itemClickListen(String ZTPJ) {
                                    ztpj.setText(ZTPJ);
                                }
                            });
                            fxlb.addView(mRiskEvaluationView);
                            //整体评价
                            ztpj.setText(mJdjclrxqBean.getRjdjhjcbInfo().getZtpj());

                            if (mJdjclrxqBean.getSgjhrList() != null && mJdjclrxqBean.getSgjhrList().size() > 0) {
                                for (JdjclrxqBean.SgjhrBean bean : mJdjclrxqBean.getSgjhrList()) {
                                    if (bean.getLrdwId().equals(MyApplication.getmLoginUser().getUserUnitId())) {
                                        mUnitPerson.add(bean);
                                    }
                                }
                            }

                            //计划监护人选择
                            jhjhr.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (jhxx.isChecked()) {
                                        showAddPopu(mUnitPerson);
                                    } else {
                                        showAddPopu2(mJdjclrxqBean.getSgjhrList());
                                    }
                                }
                            });
                            /**
                             * 新增检查单
                             *
                             *根据检查表主键是否为null 判断是否为草稿  jdjhjcbid为null 新增检查单  否则不是新增
                             * */
                            //检查表主键为null 新增检查单
                            if (mJdjclrxqBean.getRjdjhjcbInfo().getJdjhjcbId() == null) {
                                //检查日期
                                jcrq.setText(mJdjclrxqBean.getRjdjhjcbInfo().getJcrq());
                                //检查地点
                                jcdd.setText(mJdjclrxqBean.getRjdjhbInfo().getSgdd());
                                //检查人 默认设置为登录用户
                                jcr.setText(MyApplication.getmLoginUser().getTrueName());
                                //计划监护人
                                jhjhr.setText(mJdjclrxqBean.getRjdjhjcbInfo().getJhjhr());
                                //检查情况
                                jcqk.setText(mJdjclrxqBean.getRjdjhjcbInfo().getJcqk());

                                bccg.setVisibility(View.VISIBLE);

                            } else {
                                if (mJdjclrxqBean.getRjdjhjcbInfo().getJdjhjcbId() != null && mJdjclrxqBean.getRjdjhjcbInfo().getJcbstatus() == 0)
                                /**
                                 * 是草稿  RjdjhjcbInfo不为null 并且jcbstatus为0
                                 * */
                                if(mJdjclrxqBean.getRjdjhjcbInfo().getJcbtype().equals("监护信息")){
                                    jhxx.setChecked(true);
                                } else {
                                    jcxx.setChecked(true);
                                }
                                    //检查日期
                                    jcrq.setText(mJdjclrxqBean.getRjdjhjcbInfo().getJcrq());
                                //检查地点
                                jcdd.setText(mJdjclrxqBean.getRjdjhbInfo().getSgdd());
                                //检查人
                                jcr.setText(mJdjclrxqBean.getRjdjhjcbInfo().getJcrName());
                                //计划监护人
                                jhjhr.setText(mJdjclrxqBean.getRjdjhjcbInfo().getJhjhr());
                                //检查情况
                                jcqk.setText(mJdjclrxqBean.getRjdjhjcbInfo().getJcqk());

                                sccg.setVisibility(View.VISIBLE);
                                bccg.setVisibility(View.VISIBLE);

                                if ((mJdjclrxqBean.getRjdjhjcbInfo().getJcbstatus() == STATIUS_TGQX_1_TGYS_BTY || mJdjclrxqBean.getRjdjhjcbInfo().getJcbstatus() == StatusUtil.STATIUS_TGQX_2_TGSH_BTY)) {
                                    /**
                                     * 停工以后返回，按照草稿处理  RjdjhjcbInfo不为null 并且jcbstatus不为0
                                     * */
                                    if(mJdjclrxqBean.getRjdjhjcbInfo().getJcbtype().equals("监护信息")){
                                        jhxx.setChecked(true);
                                    } else {
                                        jcxx.setChecked(true);
                                    }
                                    //检查日期
                                    jcrq.setText(mJdjclrxqBean.getRjdjhjcbInfo().getJcrq());
                                    //检查地点
                                    jcdd.setText(mJdjclrxqBean.getRjdjhbInfo().getSgdd());
                                    //检查人
                                    jcr.setText(mJdjclrxqBean.getRjdjhjcbInfo().getJcrName());
                                    //计划监护人
                                    jhjhr.setText(mJdjclrxqBean.getRjdjhjcbInfo().getJhjhr());
                                    //检查情况
                                    jcqk.setText(mJdjclrxqBean.getRjdjhjcbInfo().getJcqk());

                                    sccg.setVisibility(View.GONE);
                                    bccg.setVisibility(View.GONE);
                                }
                            }

                            //根据jcbstatus是否为0或150或160 草稿状态 停工预审不同意，停工审核不同意 流转信息显示
                            if (mJdjclrxqBean.getRjdjhjcbInfo().getJcbstatus() != 0) {
                                if (mJdjclrxqBean.getRjdjhjcbInfo().getJcbstatus() == StatusUtil.STATIUS_TGQX_1_TGYS_BTY || mJdjclrxqBean.getRjdjhjcbInfo().getJcbstatus() == StatusUtil.STATIUS_TGQX_2_TGSH_BTY || mJdjclrxqBean.getRjdjhjcbInfo().getJcbstatus() != StatusUtil.STATIUS_TGQX_2_TGSH_BTY) {
                                    radiogroup_layout.setVisibility(View.VISIBLE);

                                    for (int j = 0; j < mJdjclrxqBean.getJcbShdwList().size(); j++) {
                                        JcbShdwListBean mBean = mJdjclrxqBean.getJcbShdwList().get(j);
                                        LzxxItemView mView = new LzxxItemView(JcxxAddActivity.this, mBean.getShrxx(), j);
                                        lzxx.addView(mView);
                                    }
                                }
                            }
                        } catch (Exception e) {
                            MyToast.showFail();
                        }
                        MyApplication.HideWaitDialog();
                    }

                    @Override
                    public void onFailure(Call<JdjclrxqBean> call, Throwable t) {
                        MyToast.showFail();
                    }
                }
        );
    }

    @Override
    protected void InitListen() {

        leftTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        radiogroup_layout.setVisibility(View.GONE);
        jcxx_button.setChecked(true);
        jcxx_button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    jcxx_layout.setVisibility(View.VISIBLE);
                    lzxx_layout.setVisibility(View.GONE);
                }
            }
        });
        lzxx_button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    lzxx_layout.setVisibility(View.VISIBLE);
                    jcxx_layout.setVisibility(View.GONE);
                }
            }
        });


        jhxx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    jcrtitle.setText("实际监护人:");

                } else {
                    jcrtitle.setText("检查人:");
                }
                //清空选中信息
                clearCheck();
            }
        });
        jhxx.setChecked(true);


//        检查日期 jcrq click事件选择日期
        jcrq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String[] date = {};
                String mYear;
                String mMonth;
                String mDay;
                //如果textview中存在日期 获取该日期显示在datapickdialog上
                if (isDate(jcrq.getText().toString())) {
                    date = jcrq.getText().toString().split("-");
                }
                //data为空 jcrq框内没有数据
                if (date.length < 1) {
                    mYear = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
                    mMonth = String.valueOf(Calendar.getInstance().get(Calendar.MONTH));
                    mDay = String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_WEEK));
                } else {
                    mYear = date[0];
                    mMonth = date[1];
                    mDay = date[2];
                }
                new DatePickerDialog(JcxxAddActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        jcrq.setText(new StringBuilder().append(year).append("-")
                                .append((month + 1) < 10 ? 0 : "")
                                .append((month + 1))
                                .append("-")
                                .append((dayOfMonth < 10) ? 0 : "")
                                .append((dayOfMonth)));
                    }
                }, Integer.parseInt(mYear), Integer.parseInt(mMonth) - 1, Integer.parseInt(mDay)).show();
            }
        });

        //删除草稿
        sccg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.ShowWaitDialog(JcxxAddActivity.this);
                Map<String, String> map = new HashMap<>();
                map.put("jdjhjcbId", jdjhjcbId);
                NetWorkApi.sccg(map, new Callback<SuccessBean>() {
                            @Override
                            public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                                try {
                                    SuccessBean mBean = response.body();
                                    if (mBean.getMsg() == null) {
                                        MyToast.showToast("服务器错误，请稍后再试");
                                        return;
                                    }
                                    if (mBean.getMsg().equals("success") && mBean.getCount() == 1) {
                                        MyToast.showToast("删除草稿成功");
                                        RefreshManager.Jdjc.RefreshJdjclrxx();
                                        RefreshManager.Jdjc.RefreshJdjcdcl();
                                        RefreshManager.Jdjc.RefreshJdjcycl();
                                        startActivity(new Intent(JcxxAddActivity.this, JclrActivity.class));
                                    } else {
                                        MyToast.showToast("删除失败");
                                    }
                                } catch (Exception e) {
                                    MyToast.showFail();
                                }
                                MyApplication.HideWaitDialog();
                            }

                            @Override
                            public void onFailure(Call<SuccessBean> call, Throwable t) {
                                MyToast.showFail();
                            }
                        }
                );
            }
        });
        //保存草稿按钮
        bccg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.ShowWaitDialog(JcxxAddActivity.this);
                //将数据填充进去
                SaveCaoGaoPost mBean = new SaveCaoGaoPost();
                if(jhxx.isChecked()){
                    mBean.setJcbtype("监护信息");
                } else {
                    mBean.setJcbtype("检查信息");
                }

                mBean.setJdjhjcbId(jdjhjcbId);
                mBean.setRjdjhbBh(rjhh.getText().toString());
                mBean.setYdjdjhbBh(ydbh);
                mBean.setLrrName(MyApplication.getmLoginUser().getTrueName());
                mBean.setJhjhr(jhjhr.getText().toString());
                mBean.setLrdwId(MyApplication.getmLoginUser().getUserUnitId());
                mBean.setZrdwId(((ZrdwData) zrdw.getSelectedItem()).getDwid());
                mBean.setZrDwMc(zrdw.getSelectedItem().toString());
                mBean.setJcrq(jcrq.getText().toString());
                mBean.setJcdd(jcdd.getText().toString());
                mBean.setJcrName(jcr.getText().toString());
                mBean.setZtpj(ztpj.getText().toString());
                if (jcqk.getText().toString().length() == 0) {
                    mBean.setJcqk(" ");
                } else {
                    mBean.setJcqk(jcqk.getText().toString());
                }
                if (jhxx.isChecked()) {
                    mBean.setJcbtype("监护信息");
                } else {
                    mBean.setJcbtype("检查信息");
                }
                mBean.setJcbstatus("0");
                //set安全风险list
                mBean.setLrrId(MyApplication.getmLoginUser().getUserDisplayName());
                List<SaveCaoGaoPost.LjyyxRjdjhjcbAqfxpj> mList = new ArrayList<SaveCaoGaoPost.LjyyxRjdjhjcbAqfxpj>();


                if (mRiskEvaluationView.getItemList().size() != 0) {
                    for (int i = 0; i < mRiskEvaluationView.getItemList().size(); i++) {
                        View mView = mRiskEvaluationView.getItemList().get(i);
                        TextView aqfxnr = (TextView) mView.findViewById(R.id.aqfxnr);
                        Spinner fxpj = (Spinner) mView.findViewById(R.id.fxpj);

                        SaveCaoGaoPost.LjyyxRjdjhjcbAqfxpj mAqfxpj = new SaveCaoGaoPost.LjyyxRjdjhjcbAqfxpj();
                        mAqfxpj.setJdjhbBh(rjdjhbBh);
                        mAqfxpj.setAqfxnr(aqfxnr.getText().toString());
                        mAqfxpj.setAqfxpj(String.valueOf(mRiskEvaluationView.getSpinnerSelectIdByString(fxpj.getSelectedItem().toString())));
                        mAqfxpj.setLrdwId(MyApplication.getmLoginUser().getUserUnitId());
                        mAqfxpj.setLrrId(MyApplication.getmLoginUser().getUserDisplayName());
                        mAqfxpj.setSgwtflId(mRiskEvaluationView.getSafetyById(i));
                        mList.add(mAqfxpj);
                    }
                }
                mBean.setJcbAqfxpjList(mList);

                //set审核单位信息
                SaveCaoGaoPost.LjyyxRjdjhjcbShdw shdw = new SaveCaoGaoPost.LjyyxRjdjhjcbShdw();
                shdw.setJdjhbBh(rjhh.getText().toString());
                shdw.setDwId(MyApplication.getmLoginUser().getUserUnitId());
                shdw.setDwmc(MyApplication.getmLoginUser().getUserUnitName());
                shdw.setShrphone(MyApplication.getmLoginUser().getUserMobilephone());
                shdw.setShrname(MyApplication.getmLoginUser().getTrueName());
                shdw.setJcbstatus("0");

                List<SaveCaoGaoPost.LjyyxRjdjhjcbShdw> MyList = new ArrayList<SaveCaoGaoPost.LjyyxRjdjhjcbShdw>();
                MyList.add(shdw);
                mBean.setJcbShdwList(MyList);
                RequestParams params = new RequestParams();
                NetWorkApi.SaveCaoGao(mBean, new Callback<SuccessBean>() {
                            @Override
                            public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                                try {
                                    MyApplication.HideWaitDialog();
                                    SuccessBean mBean = response.body();

                                    if (mBean.getMsg().equals("success") && mBean.getCount() == 1) {
                                        MyToast.showToast("保存草稿成功");
                                        RefreshManager.Jdjc.RefreshJdjclrxx();
                                        RefreshManager.Jdjc.RefreshJdjcdcl();
                                        RefreshManager.Jdjc.RefreshJdjcycl();
                                        Intent intent = new Intent(JcxxAddActivity.this, JclrActivity.class);
                                        intent.putExtra("code", 1);
                                        startActivity(intent);
//                            startActivity(new Intent(JcxxAddActivity.this,JclrActivity.class));
                                    } else {
                                        MyToast.showToast("提交失败");
                                    }
                                } catch (Exception e) {
                                    MyToast.showFail();
                                }
                                MyApplication.HideWaitDialog();
                            }

                            @Override
                            public void onFailure(Call<SuccessBean> call, Throwable t) {
                                MyToast.showFail();
                            }
                        }
                );


            }
        });
        //提交并整改
        tjbzg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isHaveEmpty()) {
                    MyToast.showToast("请将信息填写完整");
                } else {
                    //将数据填充进去
                    final SaveCaoGaoPost mBean = new SaveCaoGaoPost();
                    //不停工 只整改
                    mBean.setSfjytg("0");
                    mBean.setJdjhjcbId(jdjhjcbId);
                    mBean.setYdjdjhbBh(ydbh);
                    mBean.setRjdjhbBh(rjhh.getText().toString());
                    mBean.setZrdwId(((ZrdwData) zrdw.getSelectedItem()).getDwid());
                    mBean.setZrDwMc(zrdw.getSelectedItem().toString());
                    mBean.setJcrq(jcrq.getText().toString());
                    mBean.setJcdd(jcdd.getText().toString());
                    mBean.setJcrName(jcr.getText().toString());
                    mBean.setZtpj(ztpj.getText().toString());
                    mBean.setJcqk(jcqk.getText().toString());
                    mBean.setLrrName(MyApplication.getmLoginUser().getTrueName());
                    mBean.setJhjhr(jhjhr.getText().toString());
                    mBean.setLrdwId(MyApplication.getmLoginUser().getUserUnitId());

                    if (jhxx.isChecked()) {
                        mBean.setJcbtype("监护信息");
                    } else {
                        mBean.setJcbtype("检查信息");
                    }

                    mBean.setLrrId(MyApplication.getmLoginUser().getUserDisplayName());
                    List<SaveCaoGaoPost.LjyyxRjdjhjcbAqfxpj> mList = new ArrayList<SaveCaoGaoPost.LjyyxRjdjhjcbAqfxpj>();
                    if (mRiskEvaluationView.getItemList().size() != 0) {
                        for (int i = 0; i < mRiskEvaluationView.getItemList().size(); i++) {
                            View mView = mRiskEvaluationView.getItemList().get(i);
                            TextView aqfxnr = (TextView) mView.findViewById(R.id.aqfxnr);
                            Spinner fxpj = (Spinner) mView.findViewById(R.id.fxpj);

                            SaveCaoGaoPost.LjyyxRjdjhjcbAqfxpj mAqfxpj = new SaveCaoGaoPost.LjyyxRjdjhjcbAqfxpj();
                            mAqfxpj.setJdjhbBh(rjdjhbBh);
                            mAqfxpj.setAqfxnr(aqfxnr.getText().toString());
                            mAqfxpj.setAqfxpj(String.valueOf(mRiskEvaluationView.getSpinnerSelectIdByString(fxpj.getSelectedItem().toString())));
                            mAqfxpj.setLrdwId(MyApplication.getmLoginUser().getUserUnitId());
                            mAqfxpj.setLrrId(MyApplication.getmLoginUser().getUserDisplayName());
                            mAqfxpj.setSgwtflId(mRiskEvaluationView.getSafetyById(i));

                            mList.add(mAqfxpj);
                        }
                    }
                    mBean.setJcbAqfxpjList(mList);


                    SaveCaoGaoPost.LjyyxRjdjhjcbShdw shdw = new SaveCaoGaoPost.LjyyxRjdjhjcbShdw();
                    shdw.setJdjhbBh(rjhh.getText().toString());
                    shdw.setDwId(MyApplication.getmLoginUser().getUserUnitId());
                    shdw.setDwmc(MyApplication.getmLoginUser().getUserUnitName());
                    shdw.setShrphone(MyApplication.getmLoginUser().getUserMobilephone());
                    shdw.setShrlogin(MyApplication.getmLoginUser().getUserDisplayName());
                    shdw.setShrname(MyApplication.getmLoginUser().getTrueName());
                    List<SaveCaoGaoPost.LjyyxRjdjhjcbShdw> MyList = new ArrayList<SaveCaoGaoPost.LjyyxRjdjhjcbShdw>();
                    MyList.add(shdw);
                    mBean.setJcbShdwList(MyList);
                    MyApplication.ShowWaitDialog(JcxxAddActivity.this);
                    Map<String, String> map = new HashMap<>();
                    map.put("bh", mBean.getRjdjhbBh());
                    NetWorkApi.getRjhSgxmList(map, new Callback<RjhsgxmList>() {
                                @Override
                                public void onResponse(Call<RjhsgxmList> call, Response<RjhsgxmList> response) {
                                    try {
                                        MyApplication.HideWaitDialog();
                                        RjhsgxmList mRjhsgxmList = response.body();
                                        //整改提交，不停工 false
                                        boolean isShutDown = false;
                                        showTjwcDialog(mBean, mRjhsgxmList, isShutDown);
                                        RefreshManager.Jdjc.RefreshJdjclrxx();
                                        RefreshManager.Jdjc.RefreshJdjcdcl();
                                        RefreshManager.Jdjc.RefreshJdjcycl();
                                    } catch (Exception e) {
                                        MyToast.showFail();
                                    }
                                    MyApplication.HideWaitDialog();
                                }

                                @Override
                                public void onFailure(Call<RjhsgxmList> call, Throwable t) {
                                    MyToast.showFail();
                                }
                            }
                    );
                }
            }
        });

        //停工整改
        tgzg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isHaveEmpty()) {
                    MyToast.showToast("请将信息填写完整");
                } else {
                    //将数据填充进去
                    final SaveCaoGaoPost mBean = new SaveCaoGaoPost();
                    //停工 整改
                    mBean.setSfjytg("1");
                    mBean.setJdjhjcbId(jdjhjcbId);
                    mBean.setYdjdjhbBh(ydbh);
                    mBean.setRjdjhbBh(rjhh.getText().toString());
                    mBean.setZrdwId(((ZrdwData) zrdw.getSelectedItem()).getDwid());
                    mBean.setZrDwMc(zrdw.getSelectedItem().toString());
                    mBean.setJcrq(jcrq.getText().toString());
                    mBean.setJcdd(jcdd.getText().toString());
                    mBean.setJcrName(jcr.getText().toString());
                    mBean.setZtpj(ztpj.getText().toString());
                    mBean.setJcqk(jcqk.getText().toString());
                    mBean.setLrrName(MyApplication.getmLoginUser().getTrueName());
                    mBean.setLrrId(MyApplication.getmLoginUser().getUserDisplayName());
                    mBean.setJhjhr(jhjhr.getText().toString());
                    mBean.setLrdwId(MyApplication.getmLoginUser().getUserUnitId());

                    if (jhxx.isChecked()) {
                        mBean.setJcbtype("监护信息");
                    } else {
                        mBean.setJcbtype("检查信息");
                    }

                    List<SaveCaoGaoPost.LjyyxRjdjhjcbAqfxpj> mList = new ArrayList<SaveCaoGaoPost.LjyyxRjdjhjcbAqfxpj>();
                    if (mRiskEvaluationView.getItemList().size() != 0) {
                        for (int i = 0; i < mRiskEvaluationView.getItemList().size(); i++) {
                            View mView = mRiskEvaluationView.getItemList().get(i);
                            TextView aqfxnr = (TextView) mView.findViewById(R.id.aqfxnr);
                            Spinner fxpj = (Spinner) mView.findViewById(R.id.fxpj);

                            SaveCaoGaoPost.LjyyxRjdjhjcbAqfxpj mAqfxpj = new SaveCaoGaoPost.LjyyxRjdjhjcbAqfxpj();
                            mAqfxpj.setJdjhbBh(rjdjhbBh);
                            mAqfxpj.setAqfxnr(aqfxnr.getText().toString());
                            mAqfxpj.setAqfxpj(String.valueOf(mRiskEvaluationView.getSpinnerSelectIdByString(fxpj.getSelectedItem().toString())));
                            mAqfxpj.setLrdwId(MyApplication.getmLoginUser().getUserUnitId());
                            mAqfxpj.setLrrId(MyApplication.getmLoginUser().getUserDisplayName());
                            mAqfxpj.setSgwtflId(mRiskEvaluationView.getSafetyById(i));

                            mList.add(mAqfxpj);
                        }
                    }
                    mBean.setJcbAqfxpjList(mList);


                    SaveCaoGaoPost.LjyyxRjdjhjcbShdw shdw = new SaveCaoGaoPost.LjyyxRjdjhjcbShdw();
                    shdw.setJdjhbBh(rjhh.getText().toString());
                    shdw.setDwId(MyApplication.getmLoginUser().getUserUnitId());
                    shdw.setDwmc(MyApplication.getmLoginUser().getUserUnitName());
                    shdw.setShrphone(MyApplication.getmLoginUser().getUserMobilephone());
                    shdw.setShrlogin(MyApplication.getmLoginUser().getUserDisplayName());
                    shdw.setShrname(MyApplication.getmLoginUser().getTrueName());
                    //set审核单位信息
                    List<SaveCaoGaoPost.LjyyxRjdjhjcbShdw> MyList = new ArrayList<SaveCaoGaoPost.LjyyxRjdjhjcbShdw>();
                    MyList.add(shdw);
                    mBean.setJcbShdwList(MyList);
                    MyApplication.ShowWaitDialog(JcxxAddActivity.this);
                    Map<String, String> map = new HashMap<>();
                    map.put("bh", mBean.getRjdjhbBh());
                    NetWorkApi.getRjhSgxmList(map, new Callback<RjhsgxmList>() {
                                @Override
                                public void onResponse(Call<RjhsgxmList> call, Response<RjhsgxmList> response) {
                                    try {
                                        MyApplication.HideWaitDialog();
                                        RjhsgxmList mRjhsgxmList = response.body();
                                        //停工整改，停工 true
                                        boolean isShutDown = true;
                                        showTjwcDialog(mBean, mRjhsgxmList, isShutDown);
                                        RefreshManager.Jdjc.RefreshJdjclrxx();
                                        RefreshManager.Jdjc.RefreshJdjcdcl();
                                        RefreshManager.Jdjc.RefreshJdjcycl();
                                    } catch (Exception e) {
                                        MyToast.showFail();
                                    }
                                    MyApplication.HideWaitDialog();
                                }

                                @Override
                                public void onFailure(Call<RjhsgxmList> call, Throwable t) {
                                    MyToast.showFail();
                                }
                            }
                    );

                }
            }
        });

        //检查完成
        jcwc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isHaveEmpty()) {
                    MyToast.showToast("请将信息填写完整");
                    return;
                }
                //将数据填充进去
                final SaveCaoGaoPost mBean = new SaveCaoGaoPost();
                mBean.setJdjhjcbId(jdjhjcbId);
                mBean.setYdjdjhbBh(ydbh);
                mBean.setRjdjhbBh(rjhh.getText().toString());
                mBean.setZrdwId(((ZrdwData) zrdw.getSelectedItem()).getDwid());
                mBean.setZrDwMc(zrdw.getSelectedItem().toString());
                mBean.setJcrq(jcrq.getText().toString());
                mBean.setJcdd(jcdd.getText().toString());
                mBean.setJcrName(jcr.getText().toString());
                mBean.setZtpj(ztpj.getText().toString());
                mBean.setJcqk(jcqk.getText().toString());
                mBean.setLrrName(MyApplication.getmLoginUser().getTrueName());
                mBean.setJhjhr(jhjhr.getText().toString());
                mBean.setLrdwId(MyApplication.getmLoginUser().getUserUnitId());

                if (jhxx.isChecked()) {
                    mBean.setJcbtype("监护信息");
                } else {
                    mBean.setJcbtype("检查信息");
                }

                mBean.setLrrId(MyApplication.getmLoginUser().getUserDisplayName());


                List<SaveCaoGaoPost.LjyyxRjdjhjcbAqfxpj> mList = new ArrayList<SaveCaoGaoPost.LjyyxRjdjhjcbAqfxpj>();
                if (mRiskEvaluationView.getItemList().size() != 0) {
                    for (int i = 0; i < mRiskEvaluationView.getItemList().size(); i++) {
                        View mView = mRiskEvaluationView.getItemList().get(i);
                        TextView aqfxnr = (TextView) mView.findViewById(R.id.aqfxnr);
                        Spinner fxpj = (Spinner) mView.findViewById(R.id.fxpj);

                        SaveCaoGaoPost.LjyyxRjdjhjcbAqfxpj mAqfxpj = new SaveCaoGaoPost.LjyyxRjdjhjcbAqfxpj();
                        mAqfxpj.setJdjhbBh(rjdjhbBh);
                        mAqfxpj.setAqfxnr(aqfxnr.getText().toString());
                        mAqfxpj.setAqfxpj(String.valueOf(mRiskEvaluationView.getSpinnerSelectIdByString(fxpj.getSelectedItem().toString())));
                        mAqfxpj.setLrdwId(MyApplication.getmLoginUser().getUserUnitId());
                        mAqfxpj.setLrrId(MyApplication.getmLoginUser().getUserDisplayName());
                        mAqfxpj.setSgwtflId(mRiskEvaluationView.getSafetyById(i));

                        mList.add(mAqfxpj);
                    }
                }
                mBean.setJcbAqfxpjList(mList);


                SaveCaoGaoPost.LjyyxRjdjhjcbShdw shdw = new SaveCaoGaoPost.LjyyxRjdjhjcbShdw();
                shdw.setJdjhbBh(rjhh.getText().toString());
                shdw.setDwId(MyApplication.getmLoginUser().getUserUnitId());
                shdw.setDwmc(MyApplication.getmLoginUser().getUserUnitName());
                shdw.setShrphone(MyApplication.getmLoginUser().getUserMobilephone());
                shdw.setShrlogin(MyApplication.getmLoginUser().getUserDisplayName());
                shdw.setShrname(MyApplication.getmLoginUser().getTrueName());
                List<SaveCaoGaoPost.LjyyxRjdjhjcbShdw> MyList = new ArrayList<SaveCaoGaoPost.LjyyxRjdjhjcbShdw>();
                MyList.add(shdw);
                mBean.setJcbShdwList(MyList);
                mIsFinishDialog = new IsFinishDialog(JcxxAddActivity.this, mBean, JcxxAddActivity.this);
                mIsFinishDialog.show();
            }
        });

        //取消录入
        qxlr.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JcxxAddActivity.this.finish();
            }
        });
    }


    //判断字符串 是否为日期
    public boolean isDate(String s) {
        boolean flag;
        String reg = "([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})";
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(s);
        flag = matcher.matches();
        return flag;
    }

    //切换监护信息跟检查信息 以后清空选中的数据
    private void clearCheck() {
        if (mJdjclrxqBean != null && mJdjclrxqBean.getSgjhrList() != null) {
            for (JdjclrxqBean.SgjhrBean bean : mJdjclrxqBean.getSgjhrList()) {
                bean.setChecked(false);
            }
            for (JdjclrxqBean.SgjhrBean bean : mUnitPerson) {
                bean.setChecked(false);
            }
            mCheckList.clear();
            jhjhr.setText("");
        }
    }

    private void showAddPopu(List<JdjclrxqBean.SgjhrBean> list) {

        if (mPopu == null) {
            window = new AutoMatchingPopuWindow();
            window.Builder(this)
                    .setOutsideTouchable(false)
//                    .setCheckedList()
                    .hideText(true)
                    .setSourceList(list)
                    .setEditTextWatcher(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {

                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                        }
                    })
                    .setAutoMatchingPopuClickListener(new AutoMatchingPopuClickListener<JdjclrxqBean.SgjhrBean>() {
                        @Override
                        public void OnAutoMatchingPopClicked(List<JdjclrxqBean.SgjhrBean> list) {
                            mCheckList = list;
                            updateTv();
                            mPopu.dismiss();
                        }
                    });
            mPopu = window.CreatePopu();
        } else {
            window.UpListViewAdapter(list);
        }
        mPopu.showAtLocation(LinearLayout, Gravity.RIGHT, 0, 0);
    }

    private void showAddPopu2(List<JdjclrxqBean.SgjhrBean> list) {

        if (mPopu2 == null) {
            window2 = new AutoMatchingPopuWindow();
            window2.Builder(this)
                    .setOutsideTouchable(false)
//                    .setCheckedList()
                    .hideText(true)
                    .setSourceList(list)
                    .setEditTextWatcher(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {

                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                        }
                    })
                    .setAutoMatchingPopuClickListener(new AutoMatchingPopuClickListener<JdjclrxqBean.SgjhrBean>() {
                        @Override
                        public void OnAutoMatchingPopClicked(List<JdjclrxqBean.SgjhrBean> list) {
                            mCheckList = list;
                            updateTv();
                            mPopu2.dismiss();
                        }
                    });
            mPopu2 = window2.CreatePopu();
        } else {
            window2.UpListViewAdapter(list);
        }
        mPopu2.showAtLocation(LinearLayout, Gravity.RIGHT, 0, 0);
    }

    //更新计划监护人text
    private void updateTv() {
        String s = mCheckList.toString();
        jhjhr.setText(s.substring(1, s.length() - 1));
    }

    private void showTjwcDialog(SaveCaoGaoPost mSaveCaoGaoPost, RjhsgxmList mRjhsgxmList, boolean isShutDown) {
        mTjwcDialog = new TjwcDialog(JcxxAddActivity.this, mSaveCaoGaoPost, mRjhsgxmList, mJdjclrxqBean, JcxxAddActivity.this);
        mTjwcDialog.isShutDown(isShutDown);
        mTjwcDialog.show();
    }


    private boolean isHaveEmpty() {
        if (jcrq.getText().toString().equals("") || jcr.getText().toString().equals("") ||
                lrr.getText().toString().equals("") || jcdd.getText().toString().equals("") ||
                ztpj.getText().toString().equals("") || jcqk.getText().toString().equals(""))
            return true;
        else return false;
    }

    //设置安全问题模块 是否显示
    private void setsafetymodule(String ZTPJ) {

        if (ZTPJ.equals("有问题")) {
            aqwt_layout.setVisibility(View.VISIBLE);
            hasProblem = true;
        } else {
            aqwt_layout.setVisibility(View.GONE);
            hasProblem = false;
        }
    }

    //获取选中的施工安全问题
    private String getSafetyProblem() {
        String Safety = "";

        return Safety;
    }

    public void hideSaveCaoGaoBut() {
        bccg.setVisibility(View.GONE);
    }

    class ZrdwData {
        String dwmc;
        String dwid;

        @Override
        public String toString() {
            return dwmc;
        }

        public String getDwid() {
            return dwid;
        }
    }


}
